package timing_wheel

import "time"

type DayLevelTimeWheel struct {
	secondTimeWheel *TimeWheel
	minuteTimeWheel *TimeWheel
	hourTimeWheel   *TimeWheel
}

func NewDayLevelTimeWheel() *DayLevelTimeWheel {
	dayLevelTimeWheel := &DayLevelTimeWheel{}
	dayLevelTimeWheel.secondTimeWheel = NewTimeWheelWithRun(time.Second, 60, true, true, nil)
	dayLevelTimeWheel.minuteTimeWheel = NewTimeWheelWithRun(time.Minute, 60, true, false, dayLevelTimeWheel.secondTimeWheel.addTaskCh)
	dayLevelTimeWheel.hourTimeWheel = NewTimeWheelWithRun(time.Hour, 24, true, false, dayLevelTimeWheel.minuteTimeWheel.addTaskCh)
	return dayLevelTimeWheel
}

func (d *DayLevelTimeWheel) AddJob(id uint64, after time.Duration, job TimeWheelJob) {
	switch {
	case after >= d.hourTimeWheel.tickMs:
		d.hourTimeWheel.addJob(id, after, job)
		return
	case after >= d.minuteTimeWheel.tickMs:
		d.minuteTimeWheel.addJob(id, after, job)
		return
	default:
		d.secondTimeWheel.addJob(id, after, job)
		return
	}
}

func (d *DayLevelTimeWheel) AddJobWithTime(hour, minute, second time.Duration, id uint64, job TimeWheelJob) {
	after := hour + minute + second
	switch {
	case after >= d.hourTimeWheel.tickMs:
		d.hourTimeWheel.addJob(id, after, job)
		return
	case after >= d.minuteTimeWheel.tickMs:
		d.minuteTimeWheel.addJob(id, after, job)
		return
	default:
		d.secondTimeWheel.addJob(id, after, job)
		return
	}
}

func (d *DayLevelTimeWheel) RemoveJob(id uint64) {
	d.hourTimeWheel.Remove(id)
	d.minuteTimeWheel.Remove(id)
	d.secondTimeWheel.Remove(id)
}
